package com.tdam2014.grupo02;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class FlickrConnect 
{
	//public static String apiKey = "a8c6c2f1a0a11a20d5a07f5f21e6924";
	public static String apiKey = "e9f5a2f0c86babbc530bb009ba417208";
	//public static String userId = "66222229@N03";
	public static String userId = "66222229%40N03";
	
    public static ArrayList<Folder> getFolders() throws JSONException, ClientProtocolException, IOException
    {
    
        ArrayList<Folder> folders = new ArrayList<Folder>();
        HttpUriRequest request = new HttpGet("https://api.flickr.com/services/rest/");
        HttpParams params = request.getParams();
        params.setParameter("?method", "flickr.photosets.getList");
        params.setParameter("api_key", apiKey);
        params.setParameter("user_id", userId);
        params.setParameter("format", "json");
        params.setParameter("nojsoncallback", 1);
        request.setParams(params);
                
        //No funciona de esa forma
        request = new HttpGet("https://api.flickr.com/services/rest/?method=flickr.photosets.getList&api_key=e9f5a2f0c86babbc530bb009ba417208&user_id=66222229%40N03&format=json&nojsoncallback=1");
        
        
        JSONObject objResponse = Http.getResponse(request);
        JSONObject jsonObject = objResponse.getJSONObject("photosets");
        JSONArray JSonFolders = jsonObject.getJSONArray("photoset");
        for (int i = 0; i<JSonFolders.length(); i++)
        {
        	folders.add(new Folder(JSonFolders.getJSONObject(i)));
        }
        return folders;
        
    }
    public static ArrayList<Picture> getPictures(String photosetId, String folderId) throws JSONException, ClientProtocolException, IOException
    {
    
        ArrayList<Picture> pics = new ArrayList<Picture>();
                                
        //No funciona de esa forma
        String url = "https://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=6e9051f41ae16f5396b78aa89bfc8086&photoset_id=";
        url +=photosetId;
        url +="&format=json&nojsoncallback=1";
        HttpUriRequest request = new HttpGet(url);
        
        
        JSONObject objResponse = Http.getResponse(request);
        JSONObject jsonObject = objResponse.getJSONObject("photoset");
        JSONArray JSonPics = jsonObject.getJSONArray("photo");
        for (int i = 0; i<JSonPics.length(); i++)
        {
        	Picture pic = new Picture(JSonPics.getJSONObject(i));
        	pic.setFolderID(Integer.parseInt(folderId));
        	pics.add(pic);
        	
        	pics.get(i).setPictureDate(getPictureDate(pics.get(i).getPictureFlickrId()));
        }
        return pics;
        
    }
    
    public static JSONObject getPictureDate(String photoId) throws JSONException, ClientProtocolException, IOException
    {
                                         
        //No funciona de esa forma
        String url = "https://api.flickr.com/services/rest/?method=flickr.photos.getInfo&api_key=6e9051f41ae16f5396b78aa89bfc8086&photo_id=";
        url+=photoId;
        url+="&format=json&nojsoncallback=1";
        HttpUriRequest request = new HttpGet(url);

        return Http.getResponse(request);
        
    }
    public static ArrayList<Comment> getPictureComments(String photoId) throws JSONException, ClientProtocolException, IOException
    {
    	ArrayList<Comment> comments = new ArrayList<Comment>();                     
        //No funciona de esa forma
        String url = "https://api.flickr.com/services/rest/?method=flickr.photos.comments.getList&api_key=6e9051f41ae16f5396b78aa89bfc8086&photo_id=";
        url+=photoId;
        url+="&format=json&nojsoncallback=1";
        HttpUriRequest request = new HttpGet(url);
        JSONObject objResponse = Http.getResponse(request);
        JSONObject jsonComments = objResponse.getJSONObject("comments");
        JSONArray JSoncomms = jsonComments.getJSONArray("comment");
        for (int i = 0; i<JSoncomms.length(); i++)
        {
        	Comment comm = new Comment(JSoncomms.getJSONObject(i));
        	comm.setCommentPhotoId(Integer.parseInt(photoId));
        	comments.add(comm);
        	
        }
        return comments;
        
    }
}
